package simulator.physengine;

import simulator.types.Vector2f;

public abstract class PhysObject implements Updateable
{
	//absolute position of object
	protected Vector2f	_position				= new Vector2f(0);

	//rotation of object
	protected float		_rotation				= 0;
	protected float		_angularVelocity		= 0;
	protected float		_angularAcceleration	= 0;

	//heading of movement
	protected float		_heading				= 0;
	protected float		_turningRate			= 0;

	//velocity, acceleration
	protected float		_velocity				= 0;
	protected float		_acceleration			= 0;

	public PhysObject()
	{

	}

	public void physUpdate(float dt)
	{
		//update velocity, angular velocity, rotation
		_velocity += _acceleration * dt;
		_angularVelocity += _angularAcceleration * dt;
		_rotation += _angularVelocity * dt;

		//update turning rate
		_heading += _turningRate * dt;

		//update position
		updatePosition(dt);
	}

	@Override
	public void update(float dt)
	{
		//nothing, extending classes can override this.
	}

	private void updatePosition(float dt)
	{
		float _speedX = (float) (Math.cos(_heading) * _velocity);
		float _speedY = (float) (Math.sin(_heading) * _velocity);

		_position.addScaled(_speedX, _speedY, dt);
	}

	public void setPosition(float x, float y)
	{
		_position.set(x, y);
	}

	public void setRotation(float rad)
	{
		_rotation = rad;
	}

	public void setAngularVelocity(float rad_sec)
	{
		_angularVelocity = rad_sec;
	}

	public void setAngularAcceleration(float rad_sec_sec)
	{
		_angularAcceleration = rad_sec_sec;
	}

	public void setHeading(float rad)
	{
		_heading = rad;
	}

	public void setTurningRate(float rad_sec)
	{
		_turningRate = rad_sec;
	}

	public void setVelocity(float px_sec)
	{
		_velocity = px_sec;
	}

	public void setAcceleration(float px_sec_sec)
	{
		_acceleration = px_sec_sec;
	}
}
